﻿using System;
using System.Linq;
using System.Web.Mvc;
using MSAcademic.v2.Web.Models.Home;

namespace MSAcademic.v2.Web.Controllers
{
    public partial class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index(IndexModel model)
        {
            ViewData["CurrentView"] = "home";
            var nesto = model.FeaturedArticlesWithAuthors;
            return View(model);
        }

        [HttpGet]
        public ActionResult SubSections(int subsectionId)
        {
            var viewModel = _subSectionViewModelFactory();
            viewModel.SubSectionId = subsectionId;
            viewModel.CurrentlySelectedTagsIds = viewModel.AllTags.Select(x => x.Id).ToList();

            ViewData["CurrentView"] = "subsections";
            return View(viewModel);
        }
        [HttpPost]
        public ActionResult SubSections(SubSectionsViewModel viewModel)
        {
           
            if (!string.IsNullOrWhiteSpace(viewModel.SelectedTagsCsv))
            {
                var filteredTags = viewModel.SelectedTagsCsv.Split(';').Select(m => Convert.ToInt32(m)).ToList();
                viewModel.CurrentlySelectedTagsIds = filteredTags;
            }
            else
            {
                viewModel.CurrentlySelectedTagsIds = viewModel.AllTags.Select(x => x.Id).ToList();
            }

            ViewData["CurrentView"] = "subsections";
            return View(viewModel);
        }

        [HttpGet]
        public ActionResult NewsContentSection()
        {
            //TODO: vidit kako rjesit rute,unaprijed definirat content sectione?
            var viewModel = _contentSectionViewModelFactory();
            viewModel.ContentSectionId = 1; //novosti imaju Id 1 amo rec

            ViewData["CurrentView"] = "news";
            return View("ContentSection", viewModel);
        }
        [HttpGet]
        public ActionResult TutorialContentSection()
        {
            //TODO: vidit kako rjesit rute,unaprijed definirat content sectione?
            var viewModel = _contentSectionViewModelFactory();
            viewModel.ContentSectionId = 3; //tutoriali imaju Id 3 amo rec

            ViewData["CurrentView"] = "tutorials";
            return View("ContentSection", viewModel);
        }

        [HttpGet]
        public ActionResult ArticlesContentSection()
        {
            //TODO: vidit kako rjesit rute,unaprijed definirat content sectione?
            var viewModel = _contentSectionViewModelFactory();
            viewModel.ContentSectionId = 2; //članci  imaju Id 2 amo rec

            ViewData["CurrentView"] = "articles";
            return View("ContentSection", viewModel);
        }
       
      
    }
    public partial class HomeController
    {
        private readonly Func<SubSectionsViewModel> _subSectionViewModelFactory;
        private readonly Func<ContentSectionViewModel> _contentSectionViewModelFactory;

        public HomeController(Func<SubSectionsViewModel> subSectionViewModelFactory, Func<ContentSectionViewModel> contentSectionViewModelFactory)
        {
            _subSectionViewModelFactory = subSectionViewModelFactory;
            _contentSectionViewModelFactory = contentSectionViewModelFactory;
        }
    }
}
